package com.cwnu.sw.cys.core.user.service.handler;

import com.cwnu.sw.cys.core.user.service.WxMsgService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

import static java.net.URLEncoder.encode;

@Component
@Slf4j
public class ScanHandler extends AbstractHandler {

    @Autowired
    private WxMsgService wxMsgService;

    /**
     * 处理微信公众号XML消息
     *
     * @param wxMpXmlMessage 接收到的微信公众号XML消息
     * @param map 存储会话信息的映射表
     * @param wxMpService 微信公众号服务
     * @param wxSessionManager 微信会话管理器
     * @return 处理后的微信公众号XML回复消息
     * @throws WxErrorException 当处理消息发生错误时抛出
     */
    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMpXmlMessage, Map<String, Object> map,
                                    WxMpService wxMpService, WxSessionManager wxSessionManager) throws WxErrorException {

        // 扫码事件处理
        log.info("扫码事件处理");
        // 获取事件KEY和发送用户
        String eventKey = wxMpXmlMessage.getEventKey();
        String fromUser = wxMpXmlMessage.getFromUser();
        log.info("扫码事件处理,eventKey:{},fromUser:{}", eventKey, fromUser);
        return wxMsgService.scan(wxMpXmlMessage);
    }

}
